﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing.plaf.basic {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import org.aswing.*;
    import org.aswing.geom.*;
    import org.aswing.plaf.*;
    import org.aswing.event.*;
    import org.aswing.graphics.*;
    import org.aswing.resizer.*;

    public class BasicFrameUI extends BaseComponentUI implements FrameUI {

        protected var frame:JFrame;
        protected var titleBar:FrameTitleBar;
        private var resizeArrowColor:ASColor;
        private var resizeArrowLightColor:ASColor;
        private var resizeArrowDarkColor:ASColor;
        protected var mouseMoveListener:Object;
        protected var boundsMC:Sprite;
        protected var flashTimer:Timer;
        private var flashing:Boolean;
        private var flashingActivedColor:Boolean;
        private var startPos:IntPoint;
        private var startMousePos:IntPoint;

        override public function installUI(_arg1:Component):void{
            this.frame = JFrame(_arg1);
            this.installDefaults();
            this.installComponents();
            this.installListeners();
        }
        protected function getPropertyPrefix():String{
            return ("Frame.");
        }
        protected function installDefaults():void{
            var _local1:String = this.getPropertyPrefix();
            LookAndFeel.installColorsAndFont(this.frame, _local1);
            LookAndFeel.installBorderAndBFDecorators(this.frame, _local1);
            LookAndFeel.installBasicProperties(this.frame, _local1);
            this.resizeArrowColor = getColor("resizeArrow");
            this.resizeArrowLightColor = getColor("resizeArrowLight");
            this.resizeArrowDarkColor = getColor("resizeArrowDark");
            var _local2:Icon = this.frame.getIcon();
            if ((_local2 is UIResource)){
                this.frame.setIcon(getIcon((this.getPropertyPrefix() + "icon")));
            };
            if ((this.frame.getResizerMargin() is UIResource)){
                this.frame.setResizerMargin(getInsets((this.getPropertyPrefix() + "resizerMargin")));
            };
        }
        protected function installComponents():void{
            var _local1:Resizer;
            if ((((this.frame.getResizer() == null)) || ((this.frame.getResizer() is UIResource)))){
                _local1 = (getInstance((this.getPropertyPrefix() + "resizer")) as Resizer);
                this.frame.setResizer(_local1);
            };
            if (!this.frame.isDragDirectlySet()){
                this.frame.setDragDirectly(getBoolean((this.getPropertyPrefix() + "dragDirectly")));
                this.frame.setDragDirectlySet(false);
            };
            this.boundsMC = new Sprite();
            this.boundsMC.name = "drag_bounds";
        }
        protected function installListeners():void{
            this.frame.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, this.__titleBarChanged);
            this.frame.addEventListener(WindowEvent.WINDOW_ACTIVATED, this.__activeChange);
            this.frame.addEventListener(WindowEvent.WINDOW_DEACTIVATED, this.__activeChange);
            this.frame.addEventListener(PopupEvent.POPUP_CLOSED, this.__frameClosed);
            this.frame.addEventListener(Event.REMOVED_FROM_STAGE, this.__frameClosed);
            this.__titleBarChanged(null);
        }
        override public function uninstallUI(_arg1:Component):void{
            this.uninstallDefaults();
            this.uninstallComponents();
            this.uninstallListeners();
        }
        protected function uninstallDefaults():void{
            LookAndFeel.uninstallBorderAndBFDecorators(this.frame);
            this.frame.filters = [];
        }
        protected function uninstallComponents():void{
            this.removeBoundsMC();
        }
        protected function uninstallListeners():void{
            this.frame.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, this.__titleBarChanged);
            this.frame.removeEventListener(WindowEvent.WINDOW_ACTIVATED, this.__activeChange);
            this.frame.removeEventListener(WindowEvent.WINDOW_DEACTIVATED, this.__activeChange);
            this.frame.removeEventListener(PopupEvent.POPUP_CLOSED, this.__frameClosed);
            this.frame.removeEventListener(Event.REMOVED_FROM_STAGE, this.__frameClosed);
            this.removeTitleBarListeners();
            if (this.flashTimer != null){
                this.flashTimer.stop();
                this.flashTimer = null;
            };
        }
        public function flashModalFrame():void{
            if (this.flashTimer == null){
                this.flashTimer = new Timer(50, 8);
                this.flashTimer.addEventListener(TimerEvent.TIMER, this.__flashTick);
                this.flashTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.__flashComplete);
            };
            this.flashing = true;
            this.flashingActivedColor = false;
            this.flashTimer.reset();
            this.flashTimer.start();
        }
        private function __flashTick(_arg1:TimerEvent):void{
            this.flashingActivedColor = !(this.flashingActivedColor);
            this.frame.repaint();
            this.titleBar.getSelf().repaint();
        }
        private function __flashComplete(_arg1:TimerEvent):void{
            this.flashing = false;
            this.frame.repaint();
            this.titleBar.getSelf().repaint();
        }
        public function isPaintActivedFrame():Boolean{
            if (this.flashing){
                return (this.flashingActivedColor);
            };
            return (this.frame.isActive());
        }
        override protected function paintBackGround(_arg1:Component, _arg2:Graphics2D, _arg3:IntRectangle):void{
        }
        private function __titleBarChanged(_arg1:PropertyChangeEvent):void{
            var _local2:Component;
            var _local3:FrameTitleBar;
            if (((!((_arg1 == null))) && (!((_arg1.getPropertyName() == JFrame.PROPERTY_TITLE_BAR))))){
                return;
            };
            if (((_arg1) && (_arg1.getOldValue()))){
                _local3 = _arg1.getOldValue();
                _local2 = _local3.getSelf();
            };
            if (_local2){
                _local2.removeEventListener(MouseEvent.MOUSE_DOWN, this.__onTitleBarPress);
                _local2.removeEventListener(ReleaseEvent.RELEASE, this.__onTitleBarRelease);
                _local2.removeEventListener(MouseEvent.DOUBLE_CLICK, this.__onTitleBarDoubleClick);
                _local2.doubleClickEnabled = false;
            };
            this.titleBar = this.frame.getTitleBar();
            this.addTitleBarListeners();
        }
        protected function addTitleBarListeners():void{
            var _local1:Component;
            if (this.titleBar){
                _local1 = this.titleBar.getSelf();
                _local1.addEventListener(MouseEvent.MOUSE_DOWN, this.__onTitleBarPress);
                _local1.addEventListener(ReleaseEvent.RELEASE, this.__onTitleBarRelease);
                _local1.doubleClickEnabled = true;
                _local1.addEventListener(MouseEvent.DOUBLE_CLICK, this.__onTitleBarDoubleClick);
            };
        }
        protected function removeTitleBarListeners():void{
            var _local1:Component;
            if (this.titleBar){
                _local1 = this.titleBar.getSelf();
                _local1.removeEventListener(MouseEvent.MOUSE_DOWN, this.__onTitleBarPress);
                _local1.removeEventListener(ReleaseEvent.RELEASE, this.__onTitleBarRelease);
                _local1.doubleClickEnabled = false;
                _local1.removeEventListener(MouseEvent.DOUBLE_CLICK, this.__onTitleBarDoubleClick);
            };
        }
        private function isMaximizedFrame():Boolean{
            var _local1:Number = this.frame.getState();
            return (((((_local1 & JFrame.MAXIMIZED_HORIZ) == JFrame.MAXIMIZED_HORIZ)) || (((_local1 & JFrame.MAXIMIZED_VERT) == JFrame.MAXIMIZED_VERT))));
        }
        private function __activeChange(_arg1:Event):void{
            this.frame.repaint();
        }
        private function __onTitleBarPress(_arg1:MouseEvent):void{
            var _local2:Rectangle;
            var _local3:Number;
            if (((!((_arg1.target == this.titleBar))) && (!((_arg1.target == this.titleBar.getLabel()))))){
                return;
            };
            if (!this.titleBar.isTitleEnabled()){
                return;
            };
            if (((this.frame.isDragable()) && (!(this.isMaximizedFrame())))){
                if (this.frame.isDragDirectly()){
                    _local2 = this.frame.getInsets().getInsideBounds(this.frame.getMaximizedBounds()).toRectangle();
                    _local3 = this.titleBar.getSelf().getHeight();
                    _local2.x = (_local2.x - (this.frame.width - _local3));
                    _local2.y = (_local2.y - this.frame.getInsets().top);
                    _local2.width = (_local2.width + (this.frame.width - (_local3 * 2)));
                    _local2.height = (_local2.height - _local3);
                    this.frame.startDrag(false, _local2);
                } else {
                    this.startMousePos = this.frame.getMousePosition();
                    this.startPos = this.frame.getLocation();
                    if (this.frame.stage){
                        this.frame.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMove, false, 0, true);
                    };
                };
            };
        }
        private function __onTitleBarRelease(_arg1:ReleaseEvent):void{
            var _local2:IntPoint;
            if (((!((_arg1.getPressTarget() == this.titleBar))) && (!((_arg1.getPressTarget() == this.titleBar.getLabel()))))){
                return;
            };
            if (!this.titleBar.isTitleEnabled()){
                return;
            };
            this.frame.stopDrag();
            if (this.frame.stage){
                this.frame.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMove);
            };
            if (((((this.frame.isDragable()) && (!(this.isMaximizedFrame())))) && (!(this.frame.isDragDirectly())))){
                _local2 = this.representMoveBounds();
                this.frame.setLocation(_local2);
                this.frame.validate();
            };
            this.removeBoundsMC();
        }
        private function __onTitleBarDoubleClick(_arg1:Event):void{
            var _local2:int;
            if (((!((_arg1.target == this.titleBar))) && (!((_arg1.target == this.titleBar.getLabel()))))){
                return;
            };
            if (!this.titleBar.isTitleEnabled()){
                return;
            };
            if (this.frame.isResizable()){
                _local2 = this.frame.getState();
                if (((((((_local2 & JFrame.MAXIMIZED_HORIZ) == JFrame.MAXIMIZED_HORIZ)) || (((_local2 & JFrame.MAXIMIZED_VERT) == JFrame.MAXIMIZED_VERT)))) || (((_local2 & JFrame.ICONIFIED) == JFrame.ICONIFIED)))){
                    this.frame.setState(JFrame.NORMAL, false);
                } else {
                    this.frame.setState(JFrame.MAXIMIZED, false);
                };
            };
        }
        private function __frameClosed(_arg1:Event):void{
            this.removeBoundsMC();
            if (this.flashTimer != null){
                this.flashTimer.stop();
                this.flashTimer = null;
            };
            if (this.frame.stage){
                this.frame.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__onMouseMove);
            };
        }
        private function removeBoundsMC():void{
            if (((!((this.frame.parent == null))) && (this.frame.parent.contains(this.boundsMC)))){
                this.frame.parent.removeChild(this.boundsMC);
            };
        }
        private function representMoveBounds(_arg1:MouseEvent=null):IntPoint{
            var _local2:DisplayObjectContainer = this.frame.parent;
            if (this.boundsMC.parent != _local2){
                _local2.addChild(this.boundsMC);
            };
            var _local3:IntPoint = this.frame.getMousePosition();
            var _local4:IntRectangle = this.frame.getComBounds();
            _local4.x = ((this.startPos.x + _local3.x) - this.startMousePos.x);
            _local4.y = ((this.startPos.y + _local3.y) - this.startMousePos.y);
            var _local5:Number = this.titleBar.getSelf().getHeight();
            var _local6:IntRectangle = this.frame.getMaximizedBounds();
            var _local7:IntPoint = _local6.leftTop();
            var _local8:IntPoint = _local6.rightTop();
            var _local9:IntPoint = _local6.leftBottom();
            if (_local4.x < ((_local7.x - _local4.width) + _local5)){
                _local4.x = ((_local7.x - _local4.width) + _local5);
            };
            if (_local4.x > (_local8.x - _local5)){
                _local4.x = (_local8.x - _local5);
            };
            if (_local4.y < _local7.y){
                _local4.y = _local7.y;
            };
            if (_local4.y > (_local9.y - _local5)){
                _local4.y = (_local9.y - _local5);
            };
            var _local10:Insets = this.frame.getResizerMargin();
            var _local11:IntRectangle = _local4.clone();
            _local4.clone().x = (_local11.x + _local10.left);
            _local11.y = (_local11.y + _local10.top);
            _local11.width = (_local11.width - _local10.getMarginWidth());
            _local11.height = (_local11.height - _local10.getMarginHeight());
            var _local12:Number = _local11.x;
            var _local13:Number = _local11.y;
            var _local14:Number = _local11.width;
            var _local15:Number = _local11.height;
            var _local16:Graphics2D = new Graphics2D(this.boundsMC.graphics);
            this.boundsMC.graphics.clear();
            _local16.drawRectangle(new Pen(this.resizeArrowLightColor, 1), (_local12 - 1), (_local13 - 1), (_local14 + 2), (_local15 + 2));
            _local16.drawRectangle(new Pen(this.resizeArrowColor, 1), _local12, _local13, _local14, _local15);
            _local16.drawRectangle(new Pen(this.resizeArrowDarkColor, 1), (_local12 + 1), (_local13 + 1), (_local14 - 2), (_local15 - 2));
            return (_local4.leftTop());
        }
        private function __onMouseMove(_arg1:MouseEvent):void{
            this.representMoveBounds(_arg1);
        }

    }
}//package org.aswing.plaf.basic 
